package heima.lm.oschina.adapter;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;

import heima.lm.oschina.BaseApplication;

/**
 * Created by herenxue on 2017/4/1.
 */

public abstract class BasePagerAdapter extends FragmentPagerAdapter {

    // 在values/Strings.xml存储标题
    private String[] contentTitles;

    public BasePagerAdapter(FragmentManager fm) {
        super(fm);
        //添加了
        contentTitles = getTabTitles();
    }

    @Override
    public int getCount() {
        return contentTitles.length;
    }

    @Override
    public Fragment getItem(int position) {
        //使用工厂生产Fragment：替换为Fragment自带的初始化反射方法
        String[] fragmentNames = getFragmentNames();
        return Fragment.instantiate(BaseApplication.mApp, fragmentNames[position], getBundle());
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        super.destroyItem(container, position, object);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return contentTitles[position];
    }

    protected abstract String[] getTabTitles();

    protected abstract String[] getFragmentNames();

    protected abstract Bundle getBundle();
}
